{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# 层和块\n",
    "\n",
    "首先，我们回顾一下多层感知机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "origin_pos": 2,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[2, 10], dtype=Float32, value=\n",
       "[[-6.79369224e-03,  7.97413464e-04,  3.23574618e-03 ...  7.16019771e-04, -1.44797785e-04,  1.57120079e-03],\n",
       " [-6.07040105e-03,  5.09693520e-03,  4.14576288e-03 ...  1.12101820e-03, -4.35058493e-04,  4.35355678e-03]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from d2l import mindspore as d2l\n",
    "import mindspore\n",
    "from mindspore import nn, ops, Tensor\n",
    "import numpy as np\n",
    "\n",
    "net = nn.SequentialCell([nn.Dense(20, 256), nn.ReLU(), nn.Dense(256, 10)])\n",
    "\n",
    "X = d2l.rand((2, 20))\n",
    "net(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "`nn.SequentialCell`定义了一种特殊的`Cell`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "自定义块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "origin_pos": 12,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "class MLP(nn.Cell):\n",
    "    # 用模型参数声明层。这里，我们声明两个全连接的层\n",
    "    def __init__(self):\n",
    "        # 调用MLP的父类Model的构造函数来执行必要的初始化。\n",
    "        # 这样，在类实例化时也可以指定其他函数参数，例如模型参数params（稍后将介绍）\n",
    "        super().__init__()\n",
    "        self.hidden = nn.Dense(20, 256)  # 隐藏层\n",
    "        self.out = nn.Dense(256, 10)  # 输出层\n",
    "\n",
    "    # 定义模型的前向传播，即如何根据输入X返回所需的模型输出\n",
    "    def construct(self, X):\n",
    "        return self.out(d2l.relu(self.hidden(X)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "实例化多层感知机的层，然后在每次调用前向传播函数时调用这些层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "origin_pos": 16,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[2, 10], dtype=Float32, value=\n",
       "[[ 1.00408215e-06, -7.51196174e-04, -1.47786422e-03 ...  9.07916750e-04,  3.31065315e-03,  3.96206928e-03],\n",
       " [-1.93189026e-03, -1.93725619e-03, -2.41200835e-03 ...  5.95276617e-03,  4.78746044e-03,  5.16231870e-03]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = MLP()\n",
    "net(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "顺序块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "origin_pos": 26,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[2, 10], dtype=Float32, value=\n",
       "[[ 1.56846642e-03, -4.25875094e-03,  5.70291071e-04 ... -1.61940954e-03,  2.20699748e-03, -5.04909083e-03],\n",
       " [-6.69776287e-04, -4.14503831e-03,  6.94219721e-04 ... -3.46967345e-03,  5.57024789e-04, -5.33907907e-03]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class MySequential(nn.Cell):\n",
    "    def __init__(self, *args):\n",
    "        super().__init__()\n",
    "        for idx, cell in enumerate(args):\n",
    "            cell.update_parameters_name(str(idx) + \".\")\n",
    "            self._cells[str(idx)] = cell\n",
    "\n",
    "    def construct(self, X):\n",
    "        for block in self._cells.values():\n",
    "            X = block(X)\n",
    "        return X\n",
    "\n",
    "net = MySequential(nn.Dense(20, 256), nn.ReLU(), nn.Dense(256, 10))\n",
    "net(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "在前向传播函数中执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "origin_pos": 34,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 0.542837)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class FixedHiddenMLP(nn.Cell):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        # 不计算梯度的随机权重参数。因此其在训练期间保持不变\n",
    "        self.rand_weight = d2l.rand((20,20))\n",
    "        self.linear = nn.Dense(20, 20)\n",
    "\n",
    "    def construct(self, X):\n",
    "        X = self.linear(X)\n",
    "        # 使用创建的常量参数以及ReLU和matmul函数\n",
    "        X = d2l.relu(d2l.matmul(X, self.rand_weight) + 1)\n",
    "        # 复用全连接层。这相当于两个全连接层共享参数。\n",
    "        X = self.linear(X)\n",
    "        # 控制流\n",
    "        while X.abs().sum() > 1:\n",
    "            X /= 2\n",
    "        return X.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "net = FixedHiddenMLP()\n",
    "net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "混合搭配各种组合块的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "origin_pos": 37,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= -0.0956512)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class NestMLP(nn.Cell):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.net = nn.SequentialCell([nn.Dense(20, 64), nn.ReLU(),\n",
    "                                      nn.Dense(64, 32), nn.ReLU()])\n",
    "        self.linear = nn.Dense(32, 16)\n",
    "\n",
    "    def construct(self, X):\n",
    "        return self.linear(self.net(X))\n",
    "\n",
    "chimera = nn.SequentialCell([NestMLP(), nn.Dense(16, 20), FixedHiddenMLP()])\n",
    "chimera(X)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}